'use strict';
const db = uniCloud.database();
const couponCollection = db.collection('xxf-coupon');
const couponUserCollection = db.collection('xxf-coupon-user');
const dbCmd = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event);
	const {
		uid,
		action
	} = event;
	if (!uid && action) {
		return {
			msg: '用户ID参数错误！',
			code: 0,
			data: null
		};
	}
	switch (action) {
		case 'getCouponCount':
			return getCouponCount(event);
		case 'getVipCouponList':
			return getVipCouponList(event);
		case 'receiveVipCoupon':
			return receiveVipCoupon(event);
		case 'useCoupon':
			return useCoupon(event);
		case 'getMyZheList':
			return getMyZheList(event);
		case 'getMyManList':
			return getMyManList(event);
		case 'getCouponList':
			return getCouponList(event);
		default:
			return {
				msg: 'Invalid action！',
					code: 0,
					data: null
			};
	}
};

async function getCouponList(params) {
	const {
		uid
	} = params;
	const couponRes = await couponUserCollection.where({
		uid,
	}).get();
	console.log('couponRes: ', couponRes);
	const listRes = await couponCollection.where({
		status: 1,
	}).get();
	console.log('listRes: ', listRes);
	let resList = [];
	if (couponRes.data && couponRes.data.length) {
		for (let i = 0; i < couponRes.data.length; i++) {
			const iItem = couponRes.data[i];
			let resItem = {
				...iItem,
				couponInfo: null,
			};
			if (listRes.data && listRes.data.length) {
				for (let j = 0; j < listRes.data.length; j++) {
					const jItem = listRes.data[j];
					if (iItem.coupon_id === jItem._id) {
						resItem.couponInfo = jItem;
					}
				}
			}
			resList.push(resItem);
		}
	}
	resList.sort((a, b) => {
		if (a.type !== b.type) {
			return a.type < b.type ? -1 : 1;
		} else if (a.couponInfo.discount !== b.couponInfo.discount) {
			return a.couponInfo.discount < b.couponInfo.discount ? -1 : 1;
		}
		return 0;
	});
	console.log('resList: ', resList);
	return {
		msg: '',
		code: 1,
		data: {
			list: resList,
		}
	};
}

async function getMyManList(params) {
	const {
		uid
	} = params;
	const manRes = await couponUserCollection.where({
		uid,
		type: 2
	}).get();
	console.log('manRes: ', manRes);
	const listRes = await couponCollection.where({
		status: 1,
	}).get();
	console.log('listRes: ', listRes);
	let resList = [];
	if (manRes.data && manRes.data.length) {
		for (let i = 0; i < manRes.data.length; i++) {
			const iItem = manRes.data[i];
			let resItem = {
				...iItem,
				couponInfo: null,
			};
			if (listRes.data && listRes.data.length) {
				for (let j = 0; j < listRes.data.length; j++) {
					const jItem = listRes.data[j];
					if (iItem.coupon_id === jItem._id) {
						resItem.couponInfo = jItem;
					}
				}
			}
			resList.push(resItem);
		}
	}
	console.log('resList: ', resList);
	return {
		msg: '',
		code: 1,
		data: {
			list: resList,
		}
	};
}

async function getMyZheList(params) {
	const {
		uid
	} = params;
	const zheRes = await couponUserCollection.where({
		uid,
		type: 1
	}).get();
	console.log('zheRes: ', zheRes);
	const listRes = await couponCollection.where({
		status: 1,
	}).get();
	console.log('listRes: ', listRes);
	let resList = [];
	if (zheRes.data && zheRes.data.length) {
		for (let i = 0; i < zheRes.data.length; i++) {
			const iItem = zheRes.data[i];
			let resItem = {
				...iItem,
				couponInfo: null,
			};
			if (listRes.data && listRes.data.length) {
				for (let j = 0; j < listRes.data.length; j++) {
					const jItem = listRes.data[j];
					if (iItem.coupon_id === jItem._id) {
						resItem.couponInfo = jItem;
					}
				}
			}
			resList.push(resItem);
		}
	}
	console.log('resList: ', resList);
	return {
		msg: '',
		code: 1,
		data: {
			list: resList,
		}
	};
}

async function useCoupon(params) {
	const {
		uid,
		id,
		coupon_id
	} = params;
	const updateRes = await couponUserCollection.where({
		uid,
		_id: id,
		coupon_id,
	}).update({
		is_used: 1,
		used_time: Date.now(),
	});
	console.log("updateRes: ", updateRes);
	if (updateRes.updated === 1) {
		return {
			msg: '',
			code: 1,
			data: {}
		}
	} else {
		return {
			msg: '更新失败！',
			code: 0,
			data: null
		}
	}
}

async function receiveVipCoupon(params) {
	const {
		uid,
		coupon_id,
		type
	} = params;
	const createRes = await couponUserCollection.add({
		is_used: 0,
		is_expired: 0,
		coupon_id,
		uid,
		type
	})
	console.log("createRes: ", createRes);
	if (createRes) {
		return {
			msg: 'success',
			code: 1,
			data: {
				...createRes
			}
		}
	} else {
		return {
			msg: 'fail',
			code: 0,
			data: null
		};
	}
}

async function getVipCouponList(params) {
	const {
		uid
	} = params;
	const listRes = await couponCollection.where({
		status: 1,
		scene: "kthy"
	}).get();
	console.log('listRes: ', listRes);
	const userRes = await couponUserCollection.where({
		uid,
	}).get();
	console.log('userRes: ', userRes);
	let resList = [];
	for (let i = 0; i < listRes.data.length; i++) {
		const iItem = listRes.data[i];
		let resItem = {
			...iItem,
			is_received: 0,
		};
		if (userRes.data && userRes.data.length) {
			for (let j = 0; j < userRes.data.length; j++) {
				const jItem = userRes.data[j];
				if (jItem.coupon_id === iItem._id) {
					resItem.is_received = 1;
				}
			}
		}
		resList.push(resItem);
	}
	console.log('resList: ', resList);
	return {
		msg: '',
		code: 1,
		data: {
			list: resList,
		}
	};
}

async function getCouponCount(params) {
	const {
		uid
	} = params;
	const zheCountRes = await couponUserCollection.where({
		uid,
		type: 1
	}).count();
	console.log('zheCountRes: ', zheCountRes);
	const manCountRes = await couponUserCollection.where({
		uid,
		type: 2
	}).count();
	console.log('manCountRes: ', manCountRes);
	return {
		msg: '',
		code: 1,
		data: {
			zheCount: zheCountRes.total || 0,
			manCount: manCountRes.total || 0,
		}
	};
}